'use strict'

angular.module('myApp.viewTaskList', ['ngRoute'])

    .controller('ViewTaskList', ['$rootScope', '$scope', '$routeParams', '$http', '$location', function ($rootScope, $scope, $routeParams, $http, $location) {

        if ($rootScope.tasks == undefined) {
            $http.get('json/' + $routeParams.userName + '.json').success(function (data) {
                $rootScope.tasks = data;

                var maxTaskId = 0;
                for (var i = 0; i < data.length; ++i) {
                    if (data[i].task_id > maxTaskId) {
                        maxTaskId = data[i].task_id;
                    }
                }
                $rootScope.taskIdSeed = maxTaskId + 1;
                $rootScope.userName = $routeParams.userName;
            });
        }

        $scope.onAddTask = function () {
            $location.path('/' + $routeParams.userName + '/addtask');
        };

    }]);